package com.gxa.admin.controller;


import com.gxa.admin.constants.SecurityConstants;
import com.gxa.admin.pojo.dto.EmpLoginDto;
import com.gxa.admin.service.impl.AuthEmpService;
import com.gxa.core.result.Result;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;
import javax.validation.Valid;

@RestController
@Slf4j
@RequestMapping("/admin/employee")
@RequiredArgsConstructor(onConstructor = @__(@Autowired))
@Api(tags = "login&auth")
public class AuthEmpController {

    private final AuthEmpService authEmpService;

    @PostMapping("/login")
    @ApiOperation("login")
    public ResponseEntity<Result<Object>> login(@RequestBody @Valid EmpLoginDto empLoginDto){
        log.info(empLoginDto.toString());
        String token = authEmpService.createToken(empLoginDto);
        log.info(token);
        Result<Object> result = new Result<>();
        result.setCode(0);
        result.setData(token);
        HttpHeaders headers = new HttpHeaders();
        headers.set(SecurityConstants.TOKEN_HEADER,token);
        return new ResponseEntity<>(result,headers, HttpStatus.OK);
    }



}

